#! /usr/bin/env python
# -*- coding: utf-8 -*-
import ConfigParser
import string
import time
from BeautifulSoup import BeautifulSoup       
import urllib2
import re
import MySQLdb
config = {
    "database.name":            "",
    "database.user":            "",
    "database.password":        "",
    "database.host":            "",
    "time.errorsleep":          15,
    "time.sleep":               3,
    }
def getpage(url):
    request_headers = { 'User-Agent': 'NothingButMe/0.0.1' }
    request = urllib2.Request(url, None, request_headers)
    urlfile = urllib2.urlopen(request)
    return urlfile.read()
def getstats(user,steamid):
    bench=time.time()
    print "Getting stats for user : %s (%s)..." % (user, steamid)
    try:
        page=getpage("http://dystopia-stats.com/player/%s" % steamid)
    except:
        return
    soup=BeautifulSoup(page)
    result=[]
    try:
        for index in soup.find('table', cellspacing="0").findAll('tr'):
            for i in index:
              m=re.search(r'<td align="right">(.*)</td>',"%s" % i)
              if m:
                 result.append(m.group(1))
        print "...OK (took %.2fs)" %(time.time()-bench)
        return (int(result[0]),result[1],int(result[2]),int(result[3]),int(result[4]),float(result[5]))
    except:
        return


try:
    con = MySQLdb.connect(host=config["database.host"], port=3306, user=config["database.user"], passwd=config["database.password"], db=config["database.name"])
    cursor = con.cursor( )
except:
    print "Wtf ? Can't connect to mysql database"

sql = "SELECT user,steamid FROM `users` WHERE steamid IS NOT NULL GROUP BY steamid"
cursor.execute(sql)

results = cursor.fetchall( )
for user in results:
    stats=None
    while (not stats):
        stats=getstats(user[0],user[1])
        if (not stats): 
            print "Error, now sleeping for %is" % config["time.errorsleep"]
            time.sleep(config["time.errorsleep"])
            print "Retrying..."
    insert="REPLACE INTO `scores` SET steamid=\"%s\", name=\"%s\", rank=%i, time=\"%s\", points=%i, frags=%i, deaths=%i, ratio=%f, date=%i" % (user[1], user[0], stats[0], stats[1], stats[2], stats[3], stats[4], stats[5], time.time())
    result=None
    while (not result):
        try:
            bench=time.time()
            result=cursor.execute(insert)
            print "Sended to MySQL database in %.2fs" %(time.time()-bench)
        except:
            result=None
        if (not result):
            print "SQL error, now sleeping for %is" % config["time.errorsleep"]
            time.sleep(config["time.errorsleep"])
            print "Retrying..."
    print "Now sleeping for %ss..." % config["time.sleep"]
    time.sleep(config["time.sleep"])
    print "User %s OK." % user[0]

    

con.close( )
print "End"